package com.chuangyang.fixboxmaster.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.android.volley.Response;
import com.chuangyang.fixboxlib.utils.StringUtils;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.MasterSkill;
import com.chuangyang.fixboxmaster.bean.MasterSkillInfo;
import com.chuangyang.fixboxmaster.bean.PinedListType;
import com.chuangyang.fixboxmaster.bean.SkillItem;
import com.chuangyang.fixboxmaster.bean.SkillList;
import com.chuangyang.fixboxmaster.service.FixBoxApi;
import com.chuangyang.fixboxmaster.service.GsonRequest;
import com.chuangyang.fixboxmaster.ui.adapter.SkillSectionedAdapter;
import com.chuangyang.fixboxmaster.ui.widgets.PinnedSectionListView;
import com.chuangyang.fixboxmaster.utils.Constants;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by vanshu on 15/7/31 14:42.
 */
public class MySkillFragment extends BaseFragment implements View.OnClickListener {
    private View mContentView;
    private SkillSectionedAdapter mAdapter;
    private ArrayList<SkillItem> mSkillItems = new ArrayList<>();

    public static MySkillFragment newInstance() {
        return new MySkillFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContentView = inflater.inflate(R.layout.fragment_my_skill, container, false);
        PinnedSectionListView mListView = (PinnedSectionListView) mContentView.findViewById(R.id.listView);
        mAdapter = new SkillSectionedAdapter(mSkillItems);
        mListView.setAdapter(mAdapter);
        Button mBtnRequestCash = (Button) mContentView.findViewById(R.id.btn_request_skill);
        mBtnRequestCash.setOnClickListener(this);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setContentView(mContentView);
        loadData();
    }

    private void loadData() {
        HashMap<String, String> param = new HashMap<>();
        addRequest(new GsonRequest<>(FixBoxApi.GET_MASTER_SKILL, param, MasterSkillInfo.class, responseListener));
    }

    private Response.Listener<MasterSkillInfo> responseListener = new Response.Listener<MasterSkillInfo>() {
        @Override
        public void onResponse(MasterSkillInfo response) {
            if (response != null && response.result != null) {
                generateSkillItems(response.result);
                mAdapter.notifyDataSetChanged();
            }
            showResult();
        }
    };

    private void generateSkillItems(MasterSkill[] skills) {

        for (MasterSkill item : skills) {
            SkillItem sectionItem = new SkillItem();
            // section
            sectionItem.type = PinedListType.SECTION;
            sectionItem.skillTitle = item.title;
            mSkillItems.add(sectionItem);
            // item
            for (String title : item.skills) {
                SkillItem priceItem = new SkillItem();
                priceItem.type = PinedListType.ITEM;
                priceItem.skillTitle = title;
                mSkillItems.add(priceItem);
            }
        }
    }




    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_request_skill:
                StringUtils.dialPhone(getActivity(), Constants.COSTOM_SERVICE_NUMBER);
                break;
        }
    }
}
